﻿using UnityEngine;
using System.Collections;
using CustomLitJson;
using UnityEngine.Assertions;
using System.Threading.Tasks;
using System;
using Sirenix.OdinInspector;

public class GameManifestManager : StuffObject<GameManifestManager>
{

    private JsonData _nativeManifest;
    public JsonData NariveManifest
    {
        get
        {
            if (_nativeManifest == null)
            {
                _nativeManifest = new JsonData();
                string jsonString = null;
                if(NativeBridge.IsSupported)
                {
                    jsonString = NativeBridge.Call("NativeLuncherManifestManager", "GetRawString");
                }
                Debug.Log("[GameManifestManager] nativeManifest: " + jsonString);
                if(!string.IsNullOrEmpty(jsonString))
                {
                    _nativeManifest = JsonMapper.Instance.ToObject(jsonString);
                }
                else
                {
                    _nativeManifest = new JsonData();
                }
            }
            return _nativeManifest;
        }
    }

    private JsonData _unityManifest;
    public JsonData UnityManifest
    {
        get
        {
            if (_unityManifest == null)
            {
                var textAsset = Resources.Load<TextAsset>("game-manifest");
                if(textAsset == null)
                {
                    throw new Exception("[GameManifestManager] unity game-manifest.json not found");
                }
                var jsonString = textAsset.text;
                Debug.Log("[GameManifestManager] unityManifest: " + jsonString);
                if (!string.IsNullOrEmpty(jsonString))
                {
                    _unityManifest = JsonMapper.Instance.ToObject(jsonString);
                }
                else
                {
                    _unityManifest = new JsonData();
                }
            }
            return _unityManifest;
        }
    }


    private JsonData _overrideManifest;
    public JsonData OverrideManifest
    {
        get
        {
            if (_overrideManifest == null)
            {
                var textAsset = Resources.Load<TextAsset>("game-manifest-override");
                if (textAsset == null)
                {
                    _overrideManifest = new JsonData();
                }
                else
                {
                    var jsonString = textAsset.text;
                    Debug.Log("[GameManifestManager] game-manifest-override: " + jsonString);
                    if (!string.IsNullOrEmpty(jsonString))
                    {
                        _overrideManifest = JsonMapper.Instance.ToObject(jsonString);
                    }
                    else
                    {
                        _overrideManifest = new JsonData();
                    }
                }
            }
            return _overrideManifest;
        }
    }

    [HideInInspector]
    public JsonData webManifest;

    public JsonData GetObjectInEditor(string key)
    {
        var textAsset = Resources.Load<TextAsset>("game-manifest");
        var jsonString = textAsset.text;
        JsonData jd = null;
        if (!string.IsNullOrEmpty(jsonString))
        {
            jd = JsonMapper.Instance.ToObject(jsonString);
        }
        else
        {
            jd = new JsonData();
        }

        var ret = jd.TryGet<JsonData>(key);
        return ret;
    }


    public JsonData GetObject(string key)
    {
        Assert.IsTrue(Application.isPlaying, "LuncherManifestManager can't running on editor script, read luncher-manifest.json directly");

        JsonData ret;

        ret = webManifest?.TryGet<JsonData>(key, null);
        if (ret != null)
        {
            return ret;
        }
        ret = NariveManifest.TryGet<JsonData>(key, null);
        if (ret != null)
        {
            return ret;
        }
        ret = OverrideManifest.TryGet<JsonData>(key, null);
        if (ret != null)
        {
            return ret;
        }
        ret = UnityManifest.TryGet<JsonData>(key, null);
        if (ret != null)
        {
            return ret;
        }

        return null;
    }

    [ShowInInspector]
    public string Get(string key, string defaultValue = "")
    {
        if(!Application.isPlaying)
        {
            var ret2 = GetInEditor(key, defaultValue);
            return ret2;
        }

        string ret = null;

        ret = webManifest?.TryGet<string>(key, null);
        if (ret != null)
        {
            return ret;
        }
        ret = NariveManifest.TryGet<string>(key, null);
        if (ret != null)
        {
            return ret;
        }

        ret = OverrideManifest.TryGet<string>(key, null);
        if (ret != null)
        {
            return ret;
        }

        ret = UnityManifest.TryGet<string>(key, null);
        if(ret != null)
        {
            return ret;
        }

        return defaultValue;
    }

    public static string GetInEditor(string key, string defaultValue = "")
    {
        var textAsset = Resources.Load<TextAsset>("game-manifest");
        var jsonString = textAsset.text;
        JsonData jd = null;
        if (!string.IsNullOrEmpty(jsonString))
        {
            jd = JsonMapper.Instance.ToObject(jsonString);
        }
        else
        {
            jd = new JsonData();
        }

        var ret = jd.TryGet<string>(key, defaultValue);
        return ret;
    }

}
